/*
 * Copyright (c) 2010, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */
package net.stuff.servoy.eclipse.plugin.utils;

/**
 * ProgressBar class used to display feedback for the long running process of generating the JSDoc
 * 
 * @author Servoy Stuff
 * http://www.servoy-stuff.net/
 *
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class ProgressBarDialog extends Dialog {
	
	private ProgressBar pb;
	private CLabel title;
	private Label label;
	private Button cancelButton;
	private boolean canceled;
	private int max;
		
	/**
	 * Constructor
	 * @param shell
	 */
	public ProgressBarDialog(final Shell shell) {
		super(shell);
        final GridLayout gridLayout = new GridLayout();
        gridLayout.verticalSpacing = 10;

        shell.setLayout(gridLayout);
        shell.setSize(483, 181);
        shell.setText("Generating JsDoc");

        final Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        composite.setLayout(new GridLayout());

        title = new CLabel(composite, SWT.NONE);
        //message.setImage(processImage);
        title.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        title.setText("1/3 - Preprocessing...");

        Composite progressBarComposite = new Composite(shell, SWT.NONE);
        progressBarComposite.setLayoutData(new GridData(GridData.FILL,
                GridData.CENTER, true, false));
        progressBarComposite.setLayout(new FillLayout());

        pb = new ProgressBar(progressBarComposite, SWT.SMOOTH);
        pb.setMaximum(100);
        pb.setSelection(0);

        label = new Label(shell, SWT.NONE);
        label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        label.setText("");
        Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
        separator.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        
        Composite cancelComposite = new Composite(shell, SWT.NONE);
        cancelComposite.setLayoutData(new GridData(GridData.END,
                GridData.CENTER, false, false));
        final GridLayout gridLayout_1 = new GridLayout();
        gridLayout_1.numColumns = 2;
        cancelComposite.setLayout(gridLayout_1);

        
        cancelButton = new Button(cancelComposite, SWT.NONE);
        cancelButton.setCursor(new Cursor(shell.getDisplay(), SWT.CURSOR_ARROW));
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                canceled = true;
            }
        });
        cancelButton.setLayoutData(new GridData(78, SWT.DEFAULT));
        cancelButton.setText("Cancel");
        cancelButton.setEnabled(true);
		
	}


	/**
	 * @param selection the selection to set
	 */
	public void setSelection(int selection) {
		pb.setSelection(selection);
	}


	/**
	 * @param max the max to set
	 */
	public void setMax(int max) {
		this.max = max;
		pb.setMaximum(max);
	}
	
	/**
	 * @return the max
	 */
	public int getMax() {
		return max;
	}

	/**
	 * @param line the line to set
	 */
	public void setLine(String line) {
		label.setText(line);
	}

	/**
	 * @param line the line to set
	 */
	public void setChapter(String chapter) {
		title.setText(chapter);
	}

	/**
	 * @param mayCancel the mayCancel to set
	 */
	public void setMayCancel(boolean mayCancel) {
		cancelButton.setEnabled(mayCancel);
	}


	/**
	 * @return the canceled
	 */
	public boolean isCanceled() {
		return canceled;
	}


}
